package com.neuedu.controller;

import com.alibaba.fastjson2.JSONObject;
import com.neuedu.base.R;
import com.neuedu.model.Buildings;
import com.neuedu.service.BuildingsService;
import com.neuedu.service.impl.BuildingsServiceImpl;
import com.neuedu.vo.PageVO;



import java.util.Map;



public class BuildingsController {
    private BuildingsService buildingsService=new BuildingsServiceImpl();


    public R page(JSONObject jsonObject){
        //String minDate,String maxDate,String likeName
        String minDate = jsonObject.getString("minDate");
        String maxDate = jsonObject.getString("maxDate");
        String likeName = jsonObject.getString("likeName");
        Integer pageNo=jsonObject.getInteger("pageNo");
        Integer pageSize=jsonObject.getInteger("pageSize");
        PageVO<Buildings> pageVO = buildingsService.selectPage(minDate, maxDate, likeName, pageNo, pageSize);

        return R.ok("分页数据查询成功").setData(pageVO);
    }
    public static final String FILE_PATH="D:\\java07";

    public R query(Map<String,String[]> map){
        Integer id = Integer.parseInt(map.get("id")[0]);

        Buildings buildings = buildingsService.queryBuildings(id);

        return R.ok("详细信息查询成功").setData(buildings);
    }





    public R del(Map<String,String[]> map){
        String [] idsStr = map.get("id");
        Integer [] ids=new Integer[idsStr.length];
        for (int i = 0; i < ids.length; i++) {
            ids[i]=Integer.parseInt(idsStr[i]);
        }
        //调用service层，把ids传入，service帮我删去数据。
        int result = buildingsService.delBuildings(ids);
        if(result>0){
            return R.ok("删除成功");
        }else{
            return R.error("删除失败");
        }
    }

    public R save(JSONObject jsonObject){
        Integer id=jsonObject.getInteger("id");
        Integer xiaoquid = jsonObject.getInteger("xiaoquid");
        String bianhao = jsonObject.getString("bianhao");
        String mingcheng = jsonObject.getString("mingcheng");
        Integer zonghushu = jsonObject.getInteger("zonghushu");
        String description = jsonObject.getString("description");

        Buildings buildings=new Buildings();
        buildings.setId(id);
        buildings.setXiaoquid(xiaoquid);
        buildings.setBianhao(bianhao);
        buildings.setMingcheng(mingcheng);
        buildings.setZonghushu(zonghushu);
        buildings.setDescription(description);
        System.out.println(buildings);
        //service方法的保存
        int result = buildingsService.saveBuildings(buildings);
        if(result>0){
            return R.ok("更新成功");
        }else{
            return R.error("更新失败");
        }

    }


    public R add(JSONObject jsonObject){
        Integer xiaoquid = jsonObject.getInteger("xiaoquid");
        String bianhao = jsonObject.getString("bianhao");
        String mingcheng = jsonObject.getString("mingcheng");
        Integer zonghushu = jsonObject.getInteger("zonghushu");
        String description = jsonObject.getString("description");

        Buildings buildings=new Buildings();
        buildings.setXiaoquid(xiaoquid);
        buildings.setBianhao(bianhao);
        buildings.setMingcheng(mingcheng);
        buildings.setZonghushu(zonghushu);
        buildings.setDescription(description);
        int result = buildingsService.addBuildings(buildings);
        if(result>0){
            return R.ok("新增成功");
        }else{
            return R.error("新增失败");
        }
    }

}
